package fr.insalyon.telecom.mgl;

import java.lang.IllegalArgumentException;

public class RomanNumberConverter
{
    public static int convert(String romanNumber)
    {
		int length = romanNumber.length();
		int nbI=0;
		int nbV=0;
		int nbX=0;
		int nbL=0;
		int nbC=0;
		int nbD=0;
		int nbM=0;
        int roman[] = new int[length];
        int result = 0;
        
		for (int i=0; i<length; i++) {
            roman[i] = testTab(romanNumber.charAt(i));
			switch (romanNumber.charAt(i))
			{
				case 'I': nbI++; break;
				case 'V': nbV++; break;
				case 'X': nbX++; break;
				case 'L': nbL++; break;
				case 'C': nbC++; break;
				case 'D': nbD++; break;
				case 'M': nbM++; break;
			}
		}
		
		if ( nbI > 3 || nbV > 3 || nbX > 3 ||nbL > 3 ||nbC > 3 ||nbD > 3 ||nbM > 4 )
		{
			throw new IllegalArgumentException("Nombre répété trop de fois");
		}
        
        int j=0;
        do
        {
            if ( j>1) {
                if ( roman[j-1] < roman[j] && ( roman[j-1]==1 || roman[j-1]==10 || roman[j-1]==100 ) ) {
                    roman[j-1]=-roman[j-1];
                }
                if ( ( roman[j-1]<roman[j] && ( roman[j-1]==5 || roman[j-1]==50 || roman[j-1]==500 ) ) ) {
                    throw new IllegalArgumentException("Erreur");
                }
                if ( (roman[j-1]==50 && roman[j]==50) || (roman[j-1]==5 && roman[j]==5) || (roman[j-1]==500 && roman[j]==500) ) {
                    throw new IllegalArgumentException("caractere de mm poid a cote");
                }
            }
            if ( j < length - 3) {
                if(( roman[j+2] > roman[j] || roman[j+3] > roman[j] ) && roman[j] != 0 ) {
                    throw new IllegalArgumentException("Erreur");
                }
            }
            j++;
        } while ( j < length );
        
        for(int k=0; k < length; k++) {
            result += roman[k];
        }
        
        return result;

    }
    
    public static int testTab(char romanNumber) {
        switch ( romanNumber) {
            case 'I' : return 1;
            case 'V' : return 5;
            case 'X' : return 10;
            case 'L' : return 50;
            case 'C' : return 100;
            case 'D' : return 500;
            case 'M' : return 1000;
            default : throw new IllegalArgumentException("Conversion impossible");
        }
    }
    
    public static String convert(int number) {

        if (number > 3999 || number <= 0) {
            throw new IllegalArgumentException("Conversion impossible");
        } else {
            return calculMil(number) + calculCent(number) + calculDiz(number) + calculUnit(number);
        }
    }
    
    private static String calculMil(int nbreArabe) {
        String result = "";
        int i;
        
        if ( nbreArabe / 1000 != 0 )
        {
            i = nbreArabe / 1000;
            
            while ( i > 0 )
            {
                result += "M";
                i--;
            }
        }
        return result;
    }
    
    private static String calculCent(int nbreArabe) {
        String result = "";
        int i;
        
        if ( ( nbreArabe % 1000 ) / 100 != 0 )
        {
            if ( ( nbreArabe % 1000 ) / 100 == 9 ) {
                result = "CM";
            } else if ( ( ( nbreArabe % 1000 ) / 100 ) <= 8 && ( ( nbreArabe % 1000 ) / 100 ) >= 6 ) {
                i = ( ( nbreArabe % 1000 ) / 100 ) - 5;
                
                result = "D";
                while ( i > 0 ) {
                    result += "C";
                    i--;
                }
            } else if ( ( nbreArabe % 1000 ) / 100 == 5 ) {
                result = "D";
            } else if ( ( nbreArabe % 1000 ) / 100 == 4 ) {
                result = "CD";
            } else if ( ( ( nbreArabe % 1000 ) / 100 ) <= 3 && ( ( nbreArabe % 1000 ) / 100 ) >= 0 ) {
                i = ( nbreArabe % 1000 ) / 100 ;
                
                while ( i > 0 ) {
                    result += "C";
                    i--;
                }
            }
        }
        return result;
    }
    
    private static String calculDiz(int nbreArabe) {
        String result = "";
        int i;
        
        if ( ( ( nbreArabe % 1000 ) % 100 ) / 10 != 0 ) {
            if ( ( ( nbreArabe % 1000 ) % 100 ) / 10 == 9 ) {
                result = "XC";
            } else if ( ( ( ( nbreArabe % 1000 ) % 100 ) / 10 ) <= 8 && ( ( ( nbreArabe % 1000 ) % 100 ) / 10 ) >=6 ) {
                i = ( ( ( nbreArabe % 1000 ) % 100 ) / 10 ) - 5;
                
                result = "L";
                while ( i > 0 ) {
                    result += "X";
                    i--;
                }
            } else if ( ( ( nbreArabe % 1000 ) % 100 ) / 10 == 5 ) {
                result = "L";
            } else if ( ( ( nbreArabe % 1000 ) % 100 ) / 10 == 4 ) {
                result = "XL";
            } else if ( ( ( ( nbreArabe % 1000 ) % 100 ) / 10 ) <= 3 && ( ( ( nbreArabe % 1000 ) % 100 ) / 10 ) >= 1 ) {
                i = ( ( nbreArabe % 1000 ) % 100 ) / 10;
                
                while ( i > 0 ) {
                    result += "X";
                    i--;
                }
            }
        }

        return result;
    }
    
    private static String calculUnit(int nbreArabe) {
        String result = "";
        int i;
        
        if ( ( ( nbreArabe % 1000 ) % 100 ) % 10 != 0 ) {
            if ( ( ( nbreArabe % 1000 ) % 100 ) % 10 == 9 ) {
                result = "IX";
            } else if ( ( ( ( nbreArabe % 1000 ) % 100 ) % 10 ) <= 8 && ( ( ( nbreArabe % 1000 ) % 100 ) % 10 ) >= 6 ) {
                i = ( ( ( nbreArabe % 1000 ) % 100 ) % 10 ) - 5;
                
                result = "V";
                while ( i > 0 ) {
                    result += "I";
                    i--;
                }
            } else if ( ( ( nbreArabe % 1000 ) % 100 ) % 10 == 5 ) {
                result += "V";
            } else if ( ( ( nbreArabe % 1000 ) % 100 ) % 10 == 4 ) {
                result = "IV";
            } else if ( ( ( ( nbreArabe % 1000 ) % 100 ) % 10 ) <= 3 && ( ( ( nbreArabe % 1000 ) % 100 ) % 10 ) >= 1) {
                i = ( ( ( nbreArabe % 1000 ) % 100 ) % 10 );
                
                while ( i > 0 ) {
                    result += "I";
                    i--;
                }
            }
        }
        
        return result;
    }
}
